/***************************************************************************
 *   Copyright (C) 2008 by BogDan Vatra                                    *
 *   bogdan@licentia.eu                                                    *
 *                                                                         *
 *   This program is free software: you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation, either version 3 of the License, or     *
 *   (at your option) any later version.                                   *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>. *
 ***************************************************************************/
#ifndef KONTAMABILKONTAMABILLOADER_H
#define KONTAMABILKONTAMABILLOADER_H

#include <QSplashScreen>
#include <QSqlQuery>

#include <QList>
#include "win32def.h"
#include "kontamabilwindow.h"

/**
	@author BogDan Vatra <taipan@omnidatagrup.ro>
*/
namespace KontAMabil
{
	class BaseInterface;
	class MenuWidgetInterface;
	class AddWidetInterface;
};

namespace SQL
{
	class Query;
};
struct KONTAMABIL_EXPORTS LoadError
{
	enum Errors{NO_ERROR, PLUNGIN_NEED_UPDATE, SELECT_SQL_ERROR, VERSION_UPDATE_ERROR, UNKNOWN_ERROR};
	QString pluginName;
	qreal version;
	QString errorMsg;
	int errorNo;
};

class KONTAMABIL_EXPORTS KontAMabilLoader
{
private:
	struct __versions__
	{
		QString version;
		QString oper;
	};
	struct __plugin__
	{
		QString name;
		QList<__versions__> versions;
	};
private:
	KontAMabilLoader();
	friend KontAMabilLoader * kontAMabil(KontAMabilLoader *);
	static bool compPlugins(KontAMabil::BaseInterface* p1, KontAMabil::BaseInterface* p2);
	static bool compMenuPlugins(KontAMabil::MenuWidgetInterface* p1, KontAMabil::MenuWidgetInterface* p2);
	void prepareQuerys(const QString & query, const QString & pluginName);
	int compVersion(const QString & ver1, const QString & ver2);
	bool addPlugin(KontAMabil::BaseInterface* plugin);
	QList<__versions__> computeVersions(const QString & versions);
	QList<__plugin__> dependencies(KontAMabil::BaseInterface* plugin);

private:
	QList<KontAMabilWindow*> m_showedBasePlugin;
	QList<KontAMabil::BaseInterface*> m_querys;
	QList<KontAMabil::BaseInterface*> m_base;
	QList<KontAMabil::MenuWidgetInterface*> m_menus;
	SQL::Query * qry;
	bool m_execUnsafeSql;
	QList<LoadError> m_errors;
	QMap<QString,QVariant> m_globalValues;

public:
	void loadPlugins(QSplashScreen *splash=0);
	bool loadQuerys(QSplashScreen *splash=0);
	const QList<LoadError> & errors();
	const QList<KontAMabil::BaseInterface*> & basePlugins();
	const QList<KontAMabil::MenuWidgetInterface*> & menuPlugins();
	KontAMabilWindow* showNextBasePlugin();
	void addGlobalValue(const QString & valueName, const QVariant & val );
	QVariant globalValue(const QString & valueName);
	const QMap<QString,QVariant> & globalValues();
	~KontAMabilLoader();
};

KONTAMABIL_EXPORTS KontAMabilLoader * kontAMabil(KontAMabilLoader * loader=0);

#endif
